//相关业务逻辑
$(function(){
	page.bind();
	page.checkRadio();
	page.getOtherDept();
});

var page={//页面总变量
		bind:function(){
			$("#saveCompanySet").bind("click",function(){
				var object=new Object();
				object.isUpdate=$("input[name='isUpdate']:checked").val();
				object.isApprovalReport=$("input[name='isApprovalReport']:checked").val();
//				object.isFeedUpdateOrder=$("input[name='isFeedUpdateOrder']:checked").val();
				object.isAutoApproval=$("input[name='isAutoApproval']:checked").val();
				object.isAutoApproval=$("input[name='isAutoApproval']:checked").val();
				object.isNeedCode=$("input[name='isNeedCode']:checked").val();
				object.inWorkShopMode=$("input[name='inWorkShopMode']:checked").val();
				object.reportDefaultValue=$("input[name='reportDefaultValue']:checked").val();
				object.purchasePrintIsNeetApproval=$("input[name='isNeedApproval']:checked").val();
				object.approvalIsCanUpdateReportPrice=$("input[name='approvalIsCanUpdateReportPrice']:checked").val();
				object.isNeedSecondApproval=$("input[name='isNeedSecondApproval']:checked").val();
				object.isMatching=$("input[name='isMatching']:checked").val();
				object.mixdCycle=$("#mixdCycle").val();
				object.isShowYltsTotalOnPurchase=$("input[name='isShowYltsTotalOnPurchase']:checked").val();
				object.isNeedApprovalByMaterial=$("input[name='isNeedApprovalByMaterial']:checked").val();
				object.canUpdatePriceAndUnit=$("input[name='canUpdatePriceAndUnit']:checked").val();
				object.canUpdateMaterialPosition=$("input[name='canUpdateMaterialPosition']:checked").val();
				object.isCanPassReport=$("input[name='isCanPassReport']:checked").val();
				var startShare=$("#startShare").val();
				var endShare=$("#endShare").val();
				if(object.isUpdate==""){
					layAlert("选择制单物料输入类型");
					return false;
				}
				if(object.isApprovalReport==""){
					layAlert("选择报工是否需要审批");
					return false;
				}
//				if(object.isFeedUpdateOrder==""){
//					layAlert("选择投料单是否可修改订单信息");
//					return false;
//				}
				if(startShare!=''){
					if(isNaN(startShare)){
						layAlert("请输入合法最小占比");
						return false;
					}
					object.startShare=startShare;
				}else{
					object.startShare=null;
				}
				if(isNaN(object.mixdCycle) || object.mixdCycle<0){
					layAlert("请输入合法配料周期");
					return false;
				}
				
				
				
				if(endShare !=''){
					if(isNaN(endShare)){
						layAlert("请输入合法最大占比");
						return false;
					}
					object.endShare=endShare;
				}else{
					object.endShare=null;
				}
				
				if(Number(object.endShare)>100){
					layAlert("最大占比不能超过100%");
					return false;
				}
				if(Number(object.startShare)<0){
					layAlert("最小占比不能小于0%");
					return false;
				}
				if(Number(object.startShare)>Number(object.endShare)){
					layAlert("最小占比不能大于最大占比");
					return false;
				}
				if(object.isShowYltsTotalOnPurchase==''){
					layAlert("选择采购单是否显示用料条数");
					return false;
				}
				ajaxUtil.ajaxRuquest("company/setCompany",object,function(data){
					layer.msg(data,{icon:1});
					page.checkRadio();
				});
			});
			
			$("#addOtherDept").bind("click",function(){
				ajaxUtil.ajaxRuquest('basicData/getProductionDepartment',null,function(data){
					var html="";
					for(var i=0;i<data.length;i++){
						html=html+"<input type='checkbox' value='"+data[i].id+"'>"+data[i].deptName+"<br/><br/>";
					}
					$("#otherCheckDiv").html(html);
					$("#otherDeptModal").modal("show");
				});
			});
			
			$("#addOtherModalBtn").bind("click",function(){
				var array=new Array();
				$("#otherDeptModal input[type='checkbox']:checked").each(function(){
					array.push($(this).val());
				});
				if(array.length<=0){
					layAlert("请选择添加的部门");
					return false;
				}
				ajaxUtil.ajaxRuquest("order/addOtherDept",{"deptArray":JSON.stringify(array)},function(data){
					$("#otherDeptModal").modal("hide");
					layer.msg(data,{icon:1});
					page.getOtherDept();
				});
			});
			/**
			 * 设置部门入库方式
			 */
			$("#setDeptReportModalBtn").bind("click",function(){
				ajaxUtil.ajaxRuquest("basicData/getProductionDepartment",null,function(data){
					var html="";
					for(var i=0;i<data.length;i++){
						var check1="";
						var check2="";
						if(data[i].inWorkShopMode==0){
							check1="checked";
						}
						if(data[i].inWorkShopMode==1){
							check2="checked";
						}
						html+="<tr>" +
								"<td><span>"+data[i].deptName+"</span>:<input type='hidden' value='"+data[i].id+"' class='depeId'/></td>" +
								"<td><input type='radio' id='radio-1' name='isUpdate"+i+"' value='0'  "+check1+"> <label for='radio-1'>报工与领料入库</label></td> " +
								"<td><input type='radio' id='radio-2' name='isUpdate"+i+"' value='1'  "+check2+"><label for='radio-2' >领料入库</label></td>" +
							  "</tr>";
					}
					$("#deptReportModalTable").html(html);
					$("#deptReportMode").modal("show");
				});
			});
			
			/**
			 *  保存部门设置
			 */
			$("#setDeptReportModalBtnCheck").bind("click",function(){
				var array=new Array();
				$("#deptReportModalTable tr").each(function(){
					var object=new Object();
					object.id=$(this).children().find("input[type='hidden']").val();
					object.inWorkShopMode=$(this).children().find("input[type=radio]:checked").val();
					array.push(object);
				});
				/**
				 * 设置部门入库方式
				 */
				ajaxUtil.ajaxRuquest("company/setDeptInWorkShopMode",{"jsonArray":JSON.stringify(array)},function(data){
					layAlert(data);
					$("#deptReportMode").modal("hide");
				});
			});
		},
		checkRadio:function(){
			ajaxUtil.ajaxRuquest("company/getCompany",null,function(data){
				$("input[name='isUpdate']:radio[value='"+data.isUpdate+"']").attr('checked','true');
				$("input[name='isApprovalReport']:radio[value='"+data.isApprovalReport+"']").attr('checked','true');
//				$("input[name='isFeedUpdateOrder']:radio[value='"+data.isFeedUpdateOrder+"']").attr('checked','true');
				$("input[name='isAutoApproval']:radio[value='"+data.isAutoApproval+"']").attr('checked','true');
				$("input[name='isNeedCode']:radio[value='"+data.isNeedCode+"']").attr('checked','true');
				$("input[name='inWorkShopMode']:radio[value='"+data.inWorkShopMode+"']").attr('checked','true');
				$("input[name='reportDefaultValue']:radio[value='"+data.reportDefaultValue+"']").attr('checked','true');
				$("input[name='isNeedApproval']:radio[value='"+data.purchasePrintIsNeetApproval+"']").attr('checked','true');
				$("input[name='isNeedSecondApproval']:radio[value='"+data.isNeedSecondApproval+"']").attr('checked','true');
				$("input[name='isMatching']:radio[value='"+data.isMatching+"']").attr('checked','true');
				$("input[name='approvalIsCanUpdateReportPrice']:radio[value='"+data.approvalIsCanUpdateReportPrice+"']").attr('checked','true');
				$("input[name='isNeedApprovalByMaterial']:radio[value='"+data.isNeedApprovalByMaterial+"']").attr('checked','true');
				$("input[name='canUpdatePriceAndUnit']:radio[value='"+data.canUpdatePriceAndUnit+"']").attr('checked','true');
				$("input[name='canUpdateMaterialPosition']:radio[value='"+data.canUpdateMaterialPosition+"']").attr('checked','true');
				$("input[name='isCanPassReport']:radio[value='"+data.isCanPassReport+"']").attr('checked','true');
				$("#startShare").val(data.startShare);
				$("#endShare").val(data.endShare);
				$("#mixdCycle").val(data.mixdCycle);
				$("input[name='isShowYltsTotalOnPurchase']:radio[value='"+data.isShowYltsTotalOnPurchase+"']").attr('checked','true');
			});
		},
		getOtherDept:function(){
			ajaxUtil.ajaxRuquest("order/getOtherDept",null,function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<span value'"+data[i].id+"'>"+data[i].deptName+"</span> &nbsp;&nbsp;&nbsp;";
				}
				$("#otherDept").html(html);
			});
		}
}
